from typing import List
from collections import deque


class Solution:
    def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
        in_degrees = [0 for _ in range(numCourses)]
        adjancy = [[] for _ in range(numCourses)]

        queue = deque()
        result = []

        for cur, pre in prerequisites:
            in_degrees[cur] += 1
            adjancy[pre].append(cur)

        for i in range(len(in_degrees)):
            if in_degrees[i] == 0:
                queue.append(i)
        while queue:
            pre = queue.popleft()
            result.append(pre)
            numCourses -= 1
            for i in adjancy[pre]:
                in_degrees[i] -= 1
                if in_degrees[i] == 0:
                    queue.append(i)
        if numCourses != 0:
            result = []
        return result
